Questions
27 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
27 / 50

What’s the difference between :focus and :focus-visible?

Understanding :focus vs :focus-visible in CSS

The :focus pseudo-class applies styles whenever an element receives focus, regardless of how the focus was triggered (keyboard, mouse, or script). In contrast, :focus-visible applies styles only when the browser determines that focus should be visibly indicated, typically for keyboard navigation.

Key Differences
  1. 1

    :focus – Styles any element that has focus, triggered by any method.

  2. 2

    :focus-visible – Styles elements only when focus should be visibly indicated (usually keyboard or accessibility-related interactions).

  3. 3

    Using :focus-visible prevents unnecessary focus styles when clicking with a mouse, improving UX.

  4. 4

    :focus-visible respects user preferences and accessibility guidelines.

Example: :focus vs :focus-visible

In this example, when you click the button with a mouse, only the :focus style may apply depending on browser defaults. When you navigate to the button using the keyboard (Tab key), the :focus-visible outline appears, providing a clear visual indicator for keyboard users without distracting mouse users.

Best Practices
  1. 1

    Use :focus-visible to provide focus indicators for keyboard and accessibility users without affecting mouse interactions.

  2. 2

    Combine :focus and :focus-visible for consistent and accessible focus styling.

  3. 3

    Test on multiple browsers, as :focus-visible support varies and may require a polyfill in older browsers.

  4. 4

    Maintain sufficient contrast and visible indicators to meet accessibility standards.

Difficulty: 6/10
Topics: focus styles, accessibility, user input detection

Scenario Questions

0-2 years experience
  1. 1

    You're building a form and notice the input outlines disappear when clicking with a mouse, but reappear when tabbing — how would you fix it so keyboard users still see focus indicators?

  2. 2

    A teammate says 'just use :focus { outline: none; }' to clean up the look — what’s wrong with that approach and what should you use instead?

2-5 years experience
  1. 1

    Our modal dialog works fine with keyboard navigation, but users complain the focus ring is too aggressive when clicking buttons — how would you debug and fix this without breaking accessibility?

  2. 2

    After updating our button component, screen reader users report they can't tell which element has focus — what might have changed, and how would you verify it's fixed?

5-8 years experience
  1. 1

    We're redesigning our design system and want to remove all default browser outlines — how would you architect a focus style system that supports both mouse and keyboard users across 50+ components without regressions?

  2. 2

    A third-party library we use overrides :focus-visible with !important — how do you detect and mitigate this without modifying the library, and what long-term risks does this introduce?

8+ years experience
  1. 1

    Our legacy app has 200+ components using custom focus styles — how would you plan and execute a migration to :focus-visible without breaking accessibility audits or user workflows across different devices and assistive tech?

  2. 2

    As head of accessibility, you're asked to approve a design that hides all focus indicators unless the user is on a keyboard — how do you evaluate this tradeoff across global user bases with varying input methods and legal compliance requirements?

Follow-up Questions

  • How would you test that your focus styles aren't breaking keyboard navigation?
  • What happens if you remove :focus entirely and only use :focus-visible?
  • Can you think of a component where :focus-visible might cause confusion for users?